package com.leesche.zcsp.node.web.controller.wechat.discount;

import java.util.List;
import java.util.Map;

import com.leesche.zcsp.node.web.vo.Principal;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.model.PubDiscount;
import com.leesche.zcsp.node.web.service.PubDiscountService;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDiscountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDiscountCondition;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;

/**
 * Created by kaider on 2016/10/14.
 */
@Controller
@RequestMapping("/wechat/discount")
public class WechatDiscountController {
	private final static Logger LOGGER = LoggerFactory.getLogger(WechatDiscountController.class);

	@Autowired
	private PubDiscountService discountService;

	@Autowired
	private PrivilegeService privilegeService;

	private final static String SHOW_VIEW = "wechat/discount/showView";
	private final static String LIST_VIEW = "wechat/discount/listView";
	private final static String ADD_VIEW = "wechat/discount/addView";
	private final static String EDIT_VIEW = "wechat/discount/editView";

	@RequestMapping(value = "/showView", method = RequestMethod.GET)
	public String showView(Model model) {
		goView(model);
		return SHOW_VIEW;
	}

	@RequestMapping(value = "/listView", method = RequestMethod.GET)
	public String listView(Model model) {
		goView(model);
		return LIST_VIEW;
	}

	@RequestMapping(value = "/addView", method = RequestMethod.GET)
	public String addView(Model model) {
		model.addAttribute("cur", "discount");
		return ADD_VIEW;
	}

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(final PubDiscount discount) {
		try {
			Long adminId = getAdminId();

			discount.setAdminid(adminId);
			discount.setDiscountname(discount.getMoney() + "元" + discount.getCoin() + "币");
			discountService.insert(discount);
			return WebResult.success().info("操作成功").toMap();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return WebResult.failure(e).toMap();
		}
	}

	@RequestMapping(value = "/{id}/editView/{index}", method = RequestMethod.GET)
	public String editView(final @PathVariable("id") Long id,
			final @PathVariable("index") int index, Model model) {
		PubDiscount discount = discountService.selectByPrimaryKey(id);
		model.addAttribute("discount", discount);
		model.addAttribute("cur", "discount");
		model.addAttribute("index", index);
		return EDIT_VIEW;
	}

	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Map<String, Object> edit(final PubDiscount discount) {
		try {
			Long adminId = getAdminId();
			discount.setAdminid(adminId);
			discount.setDiscountname(discount.getMoney() + "元" + discount.getCoin() + "币");
			discountService.updateByPrimaryKeySelective(discount);
			return WebResult.success().info("操作成功").toMap();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return WebResult.failure(e).toMap();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/editBatch", method = RequestMethod.POST)
	public Map<String, Object> editBatch(Long[] discountid, Integer[] money,
			Integer[] coin) {
		try {
			for (int i = 0; i < discountid.length; i++) {
				PubDiscount discount = discountService.selectByPrimaryKey(discountid[i]);
				discount.setDiscountname(money[i] + "元" + coin[i] + "币");
				discount.setMoney(money[i]);
				discount.setCoin(coin[i]);
				discount.setAdminid(privilegeService.getCurrentPrincipal().getParentid());
				discountService.updateByPrimaryKeySelective(discount);
			}
			return WebResult.success().info("操作成功").toMap();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return WebResult.failure(e).toMap();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Map<String, Object> delete(final @Param(value = "discountId") Long discountId) {
		try {
			discountService.deleteByPrimaryKey(discountId);
			return WebResult.success().info("操作成功").toMap();
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			return WebResult.failure(e).toMap();
		}
	}

	/**
	 * 套餐显示
	 * @param model
	 */
	private void goView(Model model){
		Long adminId = getAdminId();
		QueryPubDiscountCondition condition = new QueryPubDiscountCondition();
		condition.setAdminId(adminId);
		List<PubDiscountDto> list = discountService.findDiscountList(condition);
		model.addAttribute("list", list);
		model.addAttribute("cur", "discount");
	}

	private Long getAdminId(){
		Principal principal = privilegeService.getCurrentPrincipal();
		Long adminId;
		if(principal.getParentid()==1){
			adminId = principal.getUserid();
		}else{
			adminId = principal.getParentid();
		}

		return adminId;
	}

}
